// JavaScript Document
$('document').ready(function()
{ 
     /* validation */
     
	 $("#register-form").validate({
      rules:
	  {
			user_name: {
		    required: true,
			minlength: 3
			},
			password: {
			required: true,
			minlength: 6,
			maxlength: 15
			},
			cpassword: {
			required: true,
			equalTo: '#password'
			},
	   },
       messages:
	   {
            user_name: "请输入用户名",
            password:{
                      required: "请输入密码",
                      minlength: "密码至少6位"
                     },
			cpassword:{
						required: "请确认密码",
						equalTo: "两次密码不符"
					  }
       },
	   submitHandler: submitForm	
       });  
	   /* validation */
	   
	   /* form submit */
	   function submitForm()
	   {		
				var data = $("#register-form").serialize();
				$.ajax({
				
				type : 'POST',
				url  : './phpservice/user/register.php',
				data : data,
				beforeSend: function()
				{	
					$("#msg").fadeOut();
					$("#btn-submit").text('loading..');
				},
				success :  function(ret)
						   {						
								console.log(ret)
								var type = {
									'registered':{
										class:'warning',
										tips:'该账户已被注册'
									},
									'initEquipFail':{
										class:'danger',
										tips:'账户初始化失败，请联系Fung'
									},
									'success':{
										class:'success',
										tips:'注册成功'
									},
									'unkown':{
										class:'danger',
										tips:'由于未知错误，注册失败'
									}
								}
								var info = type[ret];
										
								$("#msg").fadeIn(1000, function(){
										
									$("#msg").html('<div class="alert alert-' + info.class + '"><span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+ info.tips +' !</div>');
											
									$("#btn-submit").text('注册');
										
					   			});
						   	}
				});
				return false;
		}
	   /* form submit */
	   
	   
	 

});